package com.hongye.why.base.net;

import android.text.TextUtils;

import com.hongye.why.app.AppManager;
import com.hongye.why.app.YFUserSession;
import com.hongye.why.base.LifeSubscriber;
import com.hongye.why.base.constans.CommonValue;

import java.net.URLEncoder;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 网络请求管理器
 */
public class OKManager {

    private static OKManager manager;
    /**
     * API请求基址
     */
    private String hostUrl;

    private final OkHttpClient okHttpClient;

    public static OKManager getManager() {
        synchronized (OKManager.class) {
            if (manager == null) {
                manager = new OKManager();
            }
            return manager;
        }
    }

    private OKManager() {
        hostUrl = CommonValue.BASE_HOST;
        okHttpClient = new OkHttpClient();
    }

    /**
     * @param lifeSubscriber LifeSubscriber
     * @return IRequest
     */
    public IRequest addRequest(LifeSubscriber lifeSubscriber, RequestParams requestParams) {
        String inerfaceUrl;
        if (TextUtils.isEmpty(requestParams.getPath())) {
            inerfaceUrl = hostUrl;
        } else {
            inerfaceUrl = hostUrl + "/" + requestParams.getPath();
        }
        Map<String, String> params = requestParams.getParams();
        FormBody.Builder paramsBuilder = new FormBody.Builder();
        if (params != null) {
            for (String key : params.keySet()) {
                paramsBuilder.add(key, params.get(key));
            }
        }

        YFUserSession userSession = AppManager.getManager().getUserSession();
        if (!TextUtils.isEmpty(userSession.getUserToken())) {
            paramsBuilder.add("accessToken", userSession.getUserToken());
        }

        RequestBody requestBody = paramsBuilder.build();
        Request request = new Request.Builder().url(inerfaceUrl).post(requestBody).build();
        OKHttpRequest request1 = new OKHttpRequest(lifeSubscriber, okHttpClient);
        request1.setRequest(request);
        return request1;
    }


    /**
     * 农事天气
     * GET 请求
     */
    public IRequest addRequest2(LifeSubscriber lifeSubscriber, String cityName) {
        try {
            String inerfaceUrl = "http://wthrcdn.etouch.cn/WeatherApi?city=" + URLEncoder.encode(cityName, "UTF-8");
            Request request = new Request.Builder().url(inerfaceUrl).build();
            OKHttpRequest request1 = new OKHttpRequest(lifeSubscriber, okHttpClient);
            request1.setRequest(request);
            return request1;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * GET请求，直接传URL(自己加的)
     * @param lifeSubscriber
     * @param url 请求地址
     * @return
     */
    public IRequest addGetRequest(LifeSubscriber lifeSubscriber, String url) {
        try {
            Request request = new Request.Builder().url(url).build();
            OKHttpRequest request1 = new OKHttpRequest(lifeSubscriber, okHttpClient);
            request1.setRequest(request);
            return request1;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
